package com.share.dgs.fetcher;

import com.netflix.graphql.dgs.DgsComponent;
import com.netflix.graphql.dgs.DgsData;
import com.netflix.graphql.dgs.InputArgument;
import com.share.dgs.model.Rating;
import lombok.Data;

/**
 * @author caifan
 * @created 2021/3/5
 * description:
 */
@DgsComponent
public class RatingMutation {
    @DgsData(parentType = "Mutation", field = "addRating")
    public Rating addRating(@InputArgument("input") RatingInput ratingInput) {
        if(ratingInput.getStars() < 0) {
            throw new IllegalArgumentException("Stars must be 1-5");
        }

        System.out.println("Rated " + ratingInput.getTitle() + " with " + ratingInput.getStars() + " stars") ;

        return new Rating(ratingInput.getStars());
    }
}

@Data
class RatingInput {
    private String title;
    private int stars;
}
